package com.chcplayer
{
import com.chcplayer.enums.CHEventName;
import com.chcplayer.events.ViewZoomEvent;
import com.chcplayer.ui.AlertSaveFileWindow;
import com.chcplayer.ui.ChToolBar;

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.geom.Point;
import flash.geom.Rectangle;

/**
 * author yh
 */
public class ChcPlayerApp extends Sprite
{
	/**
	 */
	private var m_nW:int;
	private var m_nH:int;
	/**
	 */
	private var m_obMainBar:ChToolBar = new ChToolBar();
	/**
	 */
	private var m_obAppModel:ChcPlayerAppModel = new ChcPlayerAppModel();
	/**
	 */
	private var m_obFileListModel:FileListModel = new FileListModel();
	/**
	 */
	private var m_obFileModel:ChcFileModel = new ChcFileModel();
	/**
	 */
	private var m_alertSaveFile:AlertSaveFileWindow = new AlertSaveFileWindow();
	/**
	 */
	private var m_obImage:ChcImage = new ChcImage();
	/**
	 */
	public function ChcPlayerApp()
	{
		this._init();
	}
//--------------------------------------------------------------------------------------------------
// private
//--------------------------------------------------------------------------------------------------
	private function _init():void
	{
		m_obFileListModel.setModel(m_obFileModel, m_obAppModel);
		m_obMainBar.setModel(m_obFileListModel);
		m_obImage.setModel(m_obFileModel);
		/*
		*/
		m_obImage.$move = m_obMainBar.$move;
		this.addChild(m_obImage);
		this.addChild(m_obMainBar);
		m_obImage.addEventListener(ViewZoomEvent.FIT_BG_SCALE, zoomFitScale_chc);
		m_obImage.addEventListener(ViewZoomEvent.FIT_FG_SCALE, zoomFitScale_chc);
		m_obImage.addEventListener(ViewZoomEvent.VIEW_ZOOM, viewZoom_chc);
		m_obAppModel.addEventListener(Event.CHANGE, change_appModel);
		m_obAppModel.addEventListener(CHEventName.EDIT_MODE_CHANGE, editModeChange_appM);
		m_obFileModel.addEventListener(Event.CHANGE, change_fileModel);
		m_obFileListModel.addEventListener(Event.CHANGE, change_fileListM);
		/*
		*/
		m_obFileModel.addEventListener(CHEventName.SCALING_CHANGE, scalingChange_model);
		m_obFileModel.addEventListener(CHEventName.BG_CHANGE, bgChange_model);
		m_obFileModel.addEventListener(CHEventName.PIC_CHANGE, picChange_model);
		m_obFileModel.addEventListener(CHEventName.BG_EX_CHANGE, bgExChange_model);
		m_obFileModel.addEventListener(CHEventName.CHC_FILE_CHANGE, fileChange_model);
		m_obFileModel.addEventListener(CHEventName.ON_FILE_SAVE, onFileSave_model);
	}
	/**
	 */
	private function viewZoom_chc(e:ViewZoomEvent):void
	{
		var nHalfW:int = m_nW >> 1;
		var nHalfH:int = (m_nH - ChToolBar.BAR_HEIGHT) >> 1;
		var nOldCenter2X:int = nHalfW - m_obImage.x;
		var nOldCenter2Y:int = nHalfH - m_obImage.y;
		var nRate:Number = e.getOldScale() /  m_obImage.scaleX;
		m_obImage.x = nHalfW - nOldCenter2X / nRate;
		m_obImage.y = nHalfH - nOldCenter2Y / nRate;
	}
	/**
	 */
	private function zoomFitScale_chc(e:ViewZoomEvent):void
	{
		if ( ViewZoomEvent.FIT_BG_SCALE == e.type )
		{
			this.bringToCenter();
		}
		else
		{
			viewZoom_chc(e);
		}
	}
	/**
	 * 保存文件时
	 */
	private function onFileSave_model(e:Event):void
	{
		/*调用模型再用事件回调
		*/
		this.setEditMode(false);
	}
	/**
	 * 打开或新建
	 */
	private function fileChange_model(e:Event):void
	{
		m_obImage.clearTempData();
		m_obImage.setScaling(m_obFileModel.getScaling() );
		m_obImage.setPicBmd(m_obFileModel.getPicBmd());
		m_obImage.setBgBmd(m_obFileModel.getBgBmd());
		m_obImage.setBgRectEx(m_obFileModel.getBgRectEx());
		m_obImage.setPicLocation(m_obFileModel.getPicLocation());
		if ( m_obImage.width > 0 )
		{
			this.bringToCenter();
		}
	} 
	/**
	 */
	private function bgExChange_model(e:Event):void
	{
		m_obImage.setBgRectEx(m_obFileModel.getBgRectEx());
	}
	/**
	 */
	private function scalingChange_model(e:Event):void
	{
		m_obImage.setScaling(m_obFileModel.getScaling() );
	}
	/**
	 */
	private function picChange_model(e:Event):void
	{
		m_obImage.setPicBmd(m_obFileModel.getPicBmd());
	}
	/**
	 */
	private function bgChange_model(e:Event):void
	{
		m_obImage.setBgBmd(m_obFileModel.getBgBmd());
	}
	/**
	 */
	private function change_fileListM(e:Event):void
	{
		this.dispatchEvent(e);
	}
	/**
	 */
	private function change_fileModel(e:Event):void
	{
		this.dispatchEvent(e);
	}
	/**
	 */
	private function editModeChange_appM(e:Event):void
	{
		m_obImage.setEditMode(m_obAppModel.getEditMode());
	}
	/**
	 */
	private function change_appModel(e:Event):void
	{
		this._update();
		this.dispatchEvent(e);
	}
	/**
	 */
	private function _update():void
	{
		this.graphics.clear();
		var nH:int = m_nH;
		if ( m_obAppModel.getMainBarIsShow() )
		{
			nH -= ChToolBar.BAR_HEIGHT;
			m_obMainBar.y = nH;
			/*
			*/
			m_obMainBar.setWidth(m_nW);
		}
		if ( m_obAppModel.getUseGridBgMode() )
		{
			this.graphics.beginBitmapFill(m_obAppModel.getGridBmd());
			this.graphics.drawRect(0, 0, m_nW, nH);
		}
		else
		{
			this.graphics.beginFill(m_obAppModel.getBgColor());
			this.graphics.drawRect(0, 0, m_nW, nH);
		}
	}
//==================================================================================================
// public
//==================================================================================================
	/**
	 */
	public function setSizeWH(nW:int, nH:int):void
	{
		m_nW = nW;
		m_nH = nH;
		this._update();
	}
	/**
	 */
	public function createNewFile():void
	{
		m_obFileListModel.createNewFile();
	}
	/**
	 */
	public function openFile(fi:File):void
	{
		m_obFileListModel.openFile(fi);
	}
	/**
	 */
	public function getNeedSaveFile():Boolean
	{
		return m_obFileModel.getNeedSaveFile();
	}
	/**
	 */
	public function getFilePath():String
	{
		return m_obFileListModel.getDocPath();
	}
	/**
	 */
	public function getDefaultBgColor():int
	{
		return m_obAppModel.getDefaultBgColor();
	}
	/**
	 */
	public function setTempBgColoring(bl:Boolean):void
	{
		m_obAppModel.setTempBgColoring(bl);
	}
	/**
	 */
	public function setTempBgColor(nC:int):void
	{
		m_obAppModel.setTempBgColor(nC);
	}
	/**
	 */
	public function setBgColor(nC:int):void
	{
		m_obAppModel.setBgColor(nC);
	}
	public function getBgColor():int
	{
		return m_obAppModel.getBgColor();
	}
	/**
	 * 设置前景图
	 */
	public function setPic(fi:File):void
	{
		m_obFileModel.setPic(fi);
	}
	/**
	 * 设置背景图
	 */
	public function setBgPic(fi:File):void
	{
		m_obFileModel.setBgPic(fi);
	}
	/**
	 */
	public function setScaling(nScal:Number):void
	{
		m_obFileModel.setScaling(nScal);
	}
	/**
	 */
	public function getScaling():Number
	{
		return m_obFileModel.getScaling();
	}
	/**
	 */
	public function closeFile():void
	{
		m_obFileListModel.closeFile();
	}
	/**
	 */
	public function setBgRectEx(rect:Rectangle):void
	{
		m_obFileModel.setBgRectEx(rect);
	}
	/**
	 */
	public function getBgRectEx():Rectangle
	{
		return m_obFileModel.getBgRectEx();
	}
	/**
	 */
	public function getDefaultPicBgColor():int
	{
		return 0xffFfff;
	}
	/**
	 */
	public function setTempPicBgColoring(bl:Boolean):void
	{
		m_obFileModel.setTempBgColoring(bl);
	}
	/**
	 */
	public function setTempPicBgColor(nC:int):void
	{
		m_obFileModel.setTempBgColor(nC);
	}
	/**
	 */
	public function setPicBgColor(nC:int):void
	{
		m_obFileModel.setBgColor(nC);
	}
	/**
	 */
	public function getPicBgColor():int
	{
		return m_obFileModel.getBgColor();
	}
	/**
	 */
	public function saveFile():void
	{
		if ( m_obFileModel.getNeedSaveFile() )
		{
			var fi:File;
			try
			{
				fi = new File( m_obFileListModel.getDocPath() );
			} 
			catch(error:Error){};
			if ( fi != null )
			{
				m_alertSaveFile.centerTo(this.stage.nativeWindow);
				m_alertSaveFile.setApp(this);
				m_alertSaveFile.setWattingFI(fi);
				m_alertSaveFile.show();
			}
			else
			{
				fi = new File();
				fi.addEventListener(Event.SELECT, select_fiSave);
				fi.addEventListener(Event.CANCEL, cancel_fiSave);
				fi.browseForSave("保存文件");
			}
		}
	}
	/**
	 * 选择路径$并保存文件
	 */
	private function select_fiSave(e:Event):void
	{
		var fi:File = File(e.currentTarget);
		if ( String(fi.extension).toLowerCase() != "chc" )
		{
			fi = fi.parent.resolvePath(fi.name + ".chc");
		}
		if ( fi.exists )
		{
			m_alertSaveFile.centerTo(this.stage.nativeWindow);
			m_alertSaveFile.setApp(this);
			m_alertSaveFile.setWattingFI(fi);
			m_alertSaveFile.show();
		}
		else
		{
			m_obFileModel.saveFile(fi);
		}
		cancel_fiSave(e);
	}
	/**
	 */
	private function cancel_fiSave(e:Event):void
	{
		var fi:File = File(e.currentTarget);
		fi.removeEventListener(Event.SELECT, select_fiSave);
		fi.removeEventListener(Event.CANCEL, cancel_fiSave);
	}
	/**
	 */
	public function forciblySaveFile(fi:File):void
	{
		m_obFileModel.saveFile(fi);
	}
	/**
	 */
	public function getUseGridBgMode():Boolean
	{
		return m_obAppModel.getUseGridBgMode();
	}
//==================================================================================================
// public
//==================================================================================================
	/**
	 */
	public function bringToCenter():void
	{
		var nX:int = (m_nW - m_obImage.width) >> 1;
		var nY:int = (m_nH - ChToolBar.BAR_HEIGHT - m_obImage.height) >> 1;
		m_obImage.x = nX < 0 ? 0 : nX;
		m_obImage.y = nY < 0 ? 0 : nY;
	}
	/**
	 */
	public function zoomIn():void
	{
		m_obImage.zoomIn();
	}
	public function zoomOut():void
	{
		m_obImage.zoomOut();
	}
	/**
	 */
	public function fitBgScale():void
	{
		m_obImage.fitBgScale();
	}
	public function fitFgScale():void
	{
		m_obImage.fitFgScale();
	}
	/**
	 */
	public function setEditMode(bl:Boolean):void
	{
		m_obAppModel.setEditMode(bl);
	}
	public function getEditMode():Boolean
	{
		return m_obAppModel.getEditMode();
	}
	/**
	 */
}
}